package cn.yang.trigger.listener;

import cn.yang.domain.activity.service.IRaffleActivitySkuStockService;
import cn.yang.types.event.BaseEvent;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @version v1.0
 * @ClassName: cn.yang.trigger.listener
 * @Description: 活动sku库存耗尽
 * @Author: YJ
 */
@Slf4j
@Component
public class ActivitySkuStockZeroCustomer {

    @Value("${spring.rabbitmq.topic.activity_sku_stock_zero}")
    private static String topic;
    @Resource
    private IRaffleActivitySkuStockService skuStock;

    @RabbitListener(queuesToDeclare = @Queue(value = "${spring.rabbitmq.topic.activity_sku_stock_zero}"))
    public void listener(String message) {
        try {
            log.info("监听活动sku消耗为0的消息 topic:{} message:{}", topic, message);
            //转换对象
            BaseEvent.EventMessage<Long> eventMessage = JSON.parseObject(message,
                    new TypeReference<BaseEvent.EventMessage<Long>>() {}.getType());
            Long sku = eventMessage.getData();
            //更新库存
            skuStock.clearActivitySkuStock(sku);
        } catch (Exception e) {
            log.error("监听活动sku库存消耗为0的消息，消费失败 topic:{} message:{}", topic,message);

        }
    }

}
